import config from '../config/main';
import KoaMqReceiver from '../modules/KoaMqReceiver';

const DEFAULTS = {};

function middleware(_app, router, opts) {
    const options = Object.assign({}, DEFAULTS, opts);

    const app = _app;
    const log = app.log.getLogger('[mqm]');

    const mq = new KoaMqReceiver(app, router, options);
    mq.connect(config.rabbit.uri, () => log.info('connect open'));

    app.mq = mq;

    return function* dispatch(next) {
        this.sendChannel = mq.getSendChannel();
        yield next;
    };
}


export default middleware;
